package org.xukai.rest.common;

/**
 * @author xukai
 * @desc
 * @date 2016-12-29-下午 6:03
 */
public class Response<T> {


    /**
     * 响应码
     */
    private int code;

    /**
     * 响应消息
     */
    private String message;

    /**
     * 响应数据
     */
    private T data;

    private String errorClazz;

    private String errorStack;

    /**
     * 默认构造函数
     */
    public Response() {
        super();
    }

    /**
     * 构造函数
     *
     * @param code    响应码
     * @param message 响应消息
     */
    public Response(int code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 构造函数
     *
     * @param code    响应码
     * @param message 响应消息
     * @param data    响应数据
     */
    public Response(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Response(int code, String message, T data, String errorClazz, String errorStack) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.errorClazz = errorClazz;
        this.errorStack = errorStack;
    }

    public Response(int code, String message, String errorClazz, String errorStack) {
        this.code = code;
        this.message = message;
        this.errorClazz = errorClazz;
        this.errorStack = errorStack;
    }

    /**
     * 响应是否成功
     */
    public boolean isSuccess() {
        return RespCode.SUCCESS.code() == code;
    }

    /**
     * 返回响应code的类型.
     */
    public RespCodeType codeType() {
        RespCode respCode = RespCode.fromCode(code);
        return respCode == null ? null : respCode.codeType();
    }

    public int getCode() {
        return code;
    }

    public Response<T> setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public Response<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    public T getData() {
        return data;
    }

    public Response<T> setData(T data) {
        this.data = data;
        return this;
    }

    public String getErrorClazz() {
        return errorClazz;
    }

    public void setErrorClazz(String errorClazz) {
        this.errorClazz = errorClazz;
    }

    public String getErrorStack() {
        return errorStack;
    }

    public void setErrorStack(String errorStack) {
        this.errorStack = errorStack;
    }

    @Override
    public String toString() {
        return "Response{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                ", errorClazz='" + errorClazz + '\'' +
                ", errorStack='" + errorStack + '\'' +
                '}';
    }
}
